﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public enum BattleCardState
{
    inHand, inBlock
}
public class BattleCard : MonoBehaviour, IPointerDownHandler
{
    public int playerID;
    public BattleCardState state = BattleCardState.inHand;

    public int AttackCount;
    private int attackCount;
    public void OnPointerDown(PointerEventData eventData)
    {
        CardDisplay cardDisplay = GetComponent<CardDisplay>();
        if (cardDisplay != null && cardDisplay.card != null)
        {
            // 读取当前卡牌的 mana 值
            int cardMana = cardDisplay.card.mana;
            //Debug.Log($"当前卡牌的 Mana 值为: {cardMana}");

            // 获取玩家和敌人的角色卡 mana 值
            CharacterCard playerCharacterCard = BattleManager.Instance.playerData.CharacterCard;
            CharacterCard enemyCharacterCard = BattleManager.Instance.enemyData.CharacterCard;
            PlayerData playerData = BattleManager.Instance.playerData;
            PlayerData enemyData = BattleManager.Instance.enemyData;
            if (playerID == 0 && playerCharacterCard.mana >= cardMana)
            {
                //Debug.Log("玩家有足够的 Mana 使用这张卡牌！");
                playerCharacterCard.mana -= cardMana; // 扣除 Mana

                // 发送攻击请求（如果是战斗卡）
                BattleManager.Instance.AttackRequst(playerID, gameObject);
            }
            else if (playerID == 1 && enemyCharacterCard.mana >= cardMana)
            {
                //Debug.Log("玩家有足够的 Mana 使用这张卡牌！");
                enemyCharacterCard.mana -= cardMana; // 扣除 Mana

                // 发送攻击请求（如果是战斗卡）
                BattleManager.Instance.AttackRequst(playerID, gameObject);
            }
            else
            {
                Debug.Log("玩家没有足够的 Mana 使用这张卡牌！");
                return; // 阻止后续逻辑
            }
        }
        ////当手牌点击时，发送攻击请求(只有是战斗卡）
        //if (state == BattleCardState.inHand)
        //{
        //    BattleManager.Instance.AttackRequst(playerID, gameObject);
        //}

    }
    public void ResetAttack()
    {
        attackCount = AttackCount;
    }
    public void CostAttackCount()
    {
        attackCount -= 1;
        if (attackCount <= 0)
        {
            // 获取当前卡牌的玩家ID
            int playerID = GetComponent<BattleCard>().playerID;

            // 将卡牌加入对应的弃牌堆
            Card card = GetComponent<CardDisplay>().card;
            if (playerID == 0) // 玩家卡牌
            {
                BattleManager.Instance.playerDiscardPile.Add(card);
                Debug.Log("卡牌已加入玩家弃牌堆");
                BattleManager.Instance.ReloadDeck(0);
            }
            else if (playerID == 1) // 敌人卡牌
            {
                BattleManager.Instance.enemyDiscardPile.Add(card);
                Debug.Log("卡牌已加入敌人弃牌堆");
                BattleManager.Instance.ReloadDeck(1);
            }

            // 销毁当前游戏对象
            Destroy(gameObject);
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        ResetAttack();
    }

    // Update is called once per frame
    void Update()
    {

    }
}
